using System.Collections.Generic;
using System.Linq;
using I2.Loc;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.UI;

public class CampaignShop : MonoBehaviour
{
	public GameObject itemListView;

	public GameObject itemViewTemplate;

	public Alert alert;

	public GameObject messageCanvas;

	public Canvas askRatingCanvas;

	public Canvas askRatingIOSCanvas;

	public bool dayCheck;

	private bool isFinished;

	public List<string> playerItemNameList = new List<string>();

	public AudioClip buyAudio;

	public AudioSource audioSource;

	public Text itemNumberTextView;

	public HorizontalLayoutGroup itemNumberTextDialog;

	public int buyMax;

	public bool allowGoldItem;

	public bool AllowGoldItem
	{
		get
		{
			return allowGoldItem;
		}
		set
		{
			allowGoldItem = value;
		}
	}

	private void Awake()
	{
		if (!Config.initialized)
		{
			new Config().Init();
		}
		isFinished = false;
		foreach (string availableItem in DO.Campaign.availableItems)
		{
			playerItemNameList.Add(availableItem);
		}
		foreach (StageItem stageItem in DO.Campaign.stageItems)
		{
			if (stageItem.usedItemName != null)
			{
				playerItemNameList.Add(stageItem.usedItemName);
			}
		}
		if (DO.Campaign.primaryWeapon != "")
		{
			playerItemNameList.Add(DO.Campaign.primaryWeapon);
		}
		if (DO.Campaign.secondaryWeapon != "")
		{
			playerItemNameList.Add(DO.Campaign.secondaryWeapon);
		}
		if (DO.Campaign.meleeWeapon != "")
		{
			playerItemNameList.Add(DO.Campaign.meleeWeapon);
		}
	}

	public void Choose(GameObject itemView, Item item, int cost, bool useGold)
	{
		if (DO.Campaign.dayShoppingItems.Count >= buyMax)
		{
			return;
		}
		if (useGold)
		{
			if (DO.Player.gold < cost)
			{
				alert.Show(LocalizationManager.GetTranslation("NotEnoughGold"));
				return;
			}
			DO.Player.gold -= cost;
			DO.Campaign.goldUsed += cost;
		}
		else
		{
			if (DO.Campaign.balance < cost)
			{
				alert.Show(LocalizationManager.GetTranslation("NotEnoughMoney"));
				return;
			}
			DO.Campaign.balance -= cost;
		}
		if (item.name == "FirstAid")
		{
			DO.Campaign.playerHealth = Mathf.Min(DO.Campaign.playerHealth + 30f, Config.playerMaxHealth);
		}
		else if (item.name == "StrengthTraining")
		{
			DO.Campaign.playerStrength = Mathf.Min(DO.Campaign.playerStrength + 0.25f, Config.playerMaxStrength);
		}
		else if (item.name == "SpeedTraining")
		{
			DO.Campaign.playerSpeed = Mathf.Min(DO.Campaign.playerSpeed + 0.25f, Config.playerMaxSpeed);
		}
		else if (item.name == "KickTraining")
		{
			DO.Campaign.playerKickTotal = Mathf.Min(DO.Campaign.playerKickTotal + 1, Config.playerMaxKickTotal);
		}
		else if (item.type == ItemType.Gold)
		{
			DO.Player.gold += int.Parse(item.title);
		}
		else
		{
			if ((bool)item.requireItem)
			{
				DO.Campaign.availableItems.Remove(item.requireItem.name);
				if (DO.Campaign.primaryWeapon == item.requireItem.name)
				{
					DO.Campaign.primaryWeapon = item.gameObject.name;
				}
				else if (DO.Campaign.secondaryWeapon == item.requireItem.name)
				{
					DO.Campaign.secondaryWeapon = item.gameObject.name;
				}
				else if (DO.Campaign.meleeWeapon == item.requireItem.name)
				{
					DO.Campaign.meleeWeapon = item.gameObject.name;
				}
			}
			for (int i = 0; i < item.quantity; i++)
			{
				DO.Campaign.availableItems.Add(item.gameObject.name);
			}
		}
		Text component = itemView.transform.Find("Action/BoughtText").gameObject.GetComponent<Text>();
		Button component2 = itemView.transform.Find("Action/MoneyButton").gameObject.GetComponent<Button>();
		Button component3 = itemView.transform.Find("Action/GoldButton").gameObject.GetComponent<Button>();
		component2.gameObject.SetActive(value: false);
		component3.gameObject.SetActive(value: false);
		component.gameObject.SetActive(value: true);
		audioSource.PlayOneShot(buyAudio);
		DO.Campaign.dayShoppingItems.Add(item.gameObject.name);
		SaveLoad.SaveString("Campaign", JsonUtility.ToJson(DO.Campaign));
		if (!DO.Player.reachedItems.Contains(item.gameObject.name))
		{
			DO.Player.reachedItems.Add(item.gameObject.name);
		}
		SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
		Analytics.CustomEvent("CampaignBuy", new Dictionary<string, object> {
		{
			"item",
			item.gameObject.name
		} });
	}

	public void Finish()
	{
		if (!isFinished)
		{
			MyAdManager.Instance.MyShowNative();
			isFinished = true;
			DO.Campaign.day++;
			DO.Campaign.dayFinish = false;
			DO.Campaign.dayShoppingFinish = true;
			SaveLoad.SaveString("Campaign", JsonUtility.ToJson(DO.Campaign));
			SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
			Next();
		}
	}

	public void Next()
	{
		Initiate.Fade("CampaignPrepare", Color.black, 5f);
	}

	private void Start()
	{
		if (DO.Campaign.day > Config.showAskRatingAfterDay && !DO.Player.askRating)
		{
			DO.Player.askRating = true;
			SaveLoad.SaveString("Player", JsonUtility.ToJson(DO.Player));
		}
		Item[] array = (from t in Resources.LoadAll("Items/", typeof(Item)).Cast<Item>().ToArray()
						orderby t.buyableAfterDay, t.cost
						select t).ToArray();
		foreach (Item _item in array)
		{
			if (!_item.showable || (_item.type == ItemType.Gold && !AllowGoldItem))
			{
				continue;
			}
			Item item = Object.Instantiate(_item);
			item.gameObject.SetActive(value: false);
			if ((bool)item.requireItem && !playerItemNameList.Contains(item.requireItem.name) && !playerItemNameList.Contains(item.prefab.name))
			{
				continue;
			}
			GameObject itemView = Object.Instantiate(itemViewTemplate, itemListView.transform);
			itemView.name = _item.name;
			GameObject gameObject = itemView.transform.Find("Pic").gameObject;
			GameObject gameObject2 = itemView.transform.Find("New").gameObject;
			Text component = itemView.transform.Find("Name").gameObject.GetComponent<Text>();
			Text component2 = itemView.transform.Find("Action/OutOfStockText").gameObject.GetComponent<Text>();
			Button component3 = itemView.transform.Find("Action/MoneyButton").gameObject.GetComponent<Button>();
			Button component4 = itemView.transform.Find("Action/GoldButton").gameObject.GetComponent<Button>();
			Text component5 = component3.transform.Find("Text").gameObject.GetComponent<Text>();
			Text component6 = component4.transform.Find("Text").gameObject.GetComponent<Text>();
			itemView.GetComponent<Button>().onClick.AddListener(delegate
			{
				ShowItemDetails(item);
			});
			if (item.buyableAfterDay == DO.Campaign.day)
			{
				gameObject2.SetActive(value: true);
			}
			Object.Destroy(gameObject.transform.GetChild(0).gameObject);
			component.text = item.Title;
			if (Application.isEditor)
			{
				component.text = item.buyableAfterDay + " - " + component.text;
			}
			if (item.quantity > 1)
			{
				component.text = component.text + " X " + item.quantity;
			}
			item.transform.SetParent(gameObject.transform, worldPositionStays: false);
			item.gameObject.SetActive(value: true);
			Vector2 vector = new Vector2(60f, 60f);
			item.transform.localScale = vector;
			item.transform.SetParent(gameObject.transform);
			if (DO.Campaign.dayShoppingItems.Contains(_item.name))
			{
				component2.gameObject.SetActive(value: true);
				continue;
			}
			if (playerItemNameList.Contains(_item.name))
			{
				component2.gameObject.SetActive(value: true);
				continue;
			}
			if (_item.unbuyableAfterDay != 0 && _item.unbuyableAfterDay < DO.Campaign.day)
			{
				component2.gameObject.SetActive(value: true);
				continue;
			}
			if (_item.name == "KickTraining" && DO.Campaign.playerKickTotal >= 5)
			{
				component2.gameObject.SetActive(value: true);
				continue;
			}
			if (item.limit != 0)
			{
				int num = 0;
				foreach (StageItem stageItem in DO.Campaign.stageItems)
				{
					if (stageItem.name == _item.name)
					{
						num++;
					}
				}
				if (num >= item.limit)
				{
					component2.gameObject.SetActive(value: true);
					continue;
				}
			}
			if ((_item.name == "FirstAid" && DO.Campaign.playerHealth >= Config.playerMaxHealth) || (_item.name == "StrengthTraining" && DO.Campaign.playerStrength >= Config.playerMaxStrength) || (_item.name == "SpeedTraining" && DO.Campaign.playerSpeed >= Config.playerMaxSpeed))
			{
				component2.gameObject.SetActive(value: true);
				continue;
			}
			component3.gameObject.SetActive(value: true);
			component5.text = "$" + item.cost.ToString("n0");
			component3.onClick.AddListener(delegate
			{
				Choose(itemView, _item, item.cost, useGold: false);
			});
			if (dayCheck && item.buyableAfterDay > DO.Campaign.day)
			{
				component5.text = null;
				component5.fontSize = 16;
				component3.transform.Find("Image").gameObject.SetActive(value: false);
				component3.transform.Find("Lock").gameObject.SetActive(value: true);
				component3.interactable = false;
			}
			if (_item.type == ItemType.Gold)
			{
				component4.gameObject.SetActive(value: false);
				continue;
			}
			component4.gameObject.SetActive(value: true);
			int goldCost = item.cost / Config.goldExRate;
			if (dayCheck && item.buyableAfterDay > DO.Campaign.day)
			{
				goldCost += Mathf.FloorToInt(goldCost * (item.buyableAfterDay - DO.Campaign.day) / 2);
			}
			component6.text = goldCost.ToString("n0");
			component4.onClick.AddListener(delegate
			{
				Choose(itemView, _item, goldCost, useGold: true);
			});
		}
	}

	public void ShowItemDetails(Item item)
	{
		string description = item.Description;
		string text = "<size=30><color=#AAA>" + item.Title + "</color></size>\n" + description + "\n\n<color=#777>" + LocalizationManager.GetTranslation("ItemType") + ":</color> " + LocalizationManager.GetTranslation(item.type.ToString()) + "\n";
		if (item.type == ItemType.Tool)
		{
			text = text + "<color=#777>" + LocalizationManager.GetTranslation("QuotaInMap") + ":</color> " + ((item.limit == 0) ? LocalizationManager.GetTranslation("Unlimited") : ((object)item.limit))?.ToString() + "\n";
		}
		else if (item.type == ItemType.Weapon)
		{
			Weapon component = item.prefab.GetComponent<Weapon>();
			string translation = LocalizationManager.GetTranslation("Manual");
			if (component.chargeConfigs.Length != 0)
			{
				translation = LocalizationManager.GetTranslation("SemiAuto");
			}
			else if (component.automatic)
			{
				translation = LocalizationManager.GetTranslation("FullAuto");
			}
			else if (component.autoCocking && component.capacity > 1)
			{
				translation = LocalizationManager.GetTranslation("SemiAuto");
			}
			text = text + "<color=#777>" + LocalizationManager.GetTranslation("Weight") + ":</color> " + item.weight + "KG\n<color=#777>" + LocalizationManager.GetTranslation("FiringMode") + ":</color> " + translation + "\n<color=#777>" + LocalizationManager.GetTranslation("Damage") + ":</color> " + component.damage + ((component.chargeConfigs.Length != 0) ? (" - " + component.chargeConfigs[component.chargeConfigs.Length - 1].damage) : "") + ((component.burstCount > 0) ? (" x " + component.burstCount) : "") + "\n<color=#777>" + LocalizationManager.GetTranslation("RateOfFire") + ":</color> " + ((component.capacity > 1) ? component.rpm.ToString("n0") : "N/A") + "\n<color=#777>" + LocalizationManager.GetTranslation("MagazineSize") + ":</color> " + component.capacity + "\n<color=#777>" + LocalizationManager.GetTranslation("Penetration") + ":</color> " + component.penetration + ((component.chargeConfigs.Length != 0) ? (" - " + component.chargeConfigs[component.chargeConfigs.Length - 1].penetration) : "") + "\n<color=#777>" + LocalizationManager.GetTranslation("StoppingPower") + ":</color> " + component.stoppingPower + ((component.chargeConfigs.Length != 0) ? (" - " + component.chargeConfigs[component.chargeConfigs.Length - 1].stoppingPower) : "") + "\n<color=#777>" + LocalizationManager.GetTranslation("SupportTurret") + ":</color> " + LocalizationManager.GetTranslation(item.turretUsable ? "Support" : "NotSupport") + "\n";
		}
		else if (item.type == ItemType.MeleeWeapon)
		{
			MeleeWeapon component2 = item.prefab.GetComponent<MeleeWeapon>();
			text = text + "<color=#777>" + LocalizationManager.GetTranslation("Weight") + ":</color> " + item.weight + "KG\n<color=#777>" + LocalizationManager.GetTranslation("Damage") + ":</color> " + component2.damage + "\n<color=#777>" + LocalizationManager.GetTranslation("Range") + ":</color> " + component2.range + "m\n<color=#777>" + LocalizationManager.GetTranslation("KnockbackPower") + ":</color> " + component2.knockbackPower + "\n<color=#777>" + LocalizationManager.GetTranslation("Decapitation") + ":</color> " + component2.decapitationChance.ToString("P0") + "\n";
		}
		else if (item.type == ItemType.Suit)
		{
			PlayerBody component3 = item.prefab.transform.GetChild(0).GetComponent<PlayerBody>();
			text = text + "<color=#777>" + LocalizationManager.GetTranslation("Armor") + ":</color> " + component3.armor.ToString("n0") + "\n<color=#777>" + LocalizationManager.GetTranslation("Strength") + ":</color> " + ((component3.strength > 0f) ? "+" : "") + component3.strength + "\n<color=#777>" + LocalizationManager.GetTranslation("Speed") + ":</color> " + ((component3.speed > 0f) ? "+" : "") + component3.speed + "\n<color=#777>" + LocalizationManager.GetTranslation("ChemicalProtection") + ":</color> " + component3.vomitResistance.ToString("P0") + "\n";
		}
		ShowMessage(text);
	}

	private void FixedUpdate()
	{
		int num = buyMax - DO.Campaign.dayShoppingItems.Count;
		if (num == buyMax)
		{
			itemNumberTextView.text = LocalizationManager.GetTranslation("ShopMessageStart").Replace("{ITEM_NUMBER}", num.ToString());
		}
		else
		{
			switch (num)
			{
				case 1:
					itemNumberTextView.text = LocalizationManager.GetTranslation("ShopMessageLast").Replace("{ITEM_NUMBER}", num.ToString());
					break;
				case 0:
					itemNumberTextView.text = LocalizationManager.GetTranslation("ShopMessageEnd").Replace("{ITEM_NUMBER}", num.ToString());
					break;
				default:
					itemNumberTextView.text = LocalizationManager.GetTranslation("ShopMessageLoop").Replace("{ITEM_NUMBER}", num.ToString());
					break;
			}
		}
		if (num == 0 && !isFinished)
		{
			Finish();
		}
		itemNumberTextDialog.enabled = false;
		itemNumberTextDialog.enabled = true;
	}

	public void ShowMessage(string text)
	{
		Canvas component = messageCanvas.GetComponent<Canvas>();
		messageCanvas.transform.Find("Text").GetComponent<Text>().text = text;
		component.enabled = true;
	}

	public void OpenURL(string url)
	{
		Application.OpenURL(url);
	}
}
